package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.HeartCheckCallback;
import com.apex.bluetooth.callback.HeartLimitCallback;
import com.apex.bluetooth.callback.TitanHeartInfoCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleHr;
import com.apex.bluetooth.model.TitanHeartMonitorInfo;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityTitanHeartRateBinding;
import com.apex.sdk.databinding.ActivityTitanOtaBinding;
import com.apex.sdk.dialog.MonthDialog;
import com.apex.sdk.dialog.NotDisturbDialog;
import com.apex.sdk.dialog.SwitchDialog;
import com.apex.sdk.dialog.WaitingDialog;


public class TitanHeartChechActivity extends AppCompatActivity {

    private WaitingDialog waitingDialog;
    private SwitchDialog heartDialog;
    private MonthDialog monthDialog, maxDialog;
    NotDisturbDialog notDisturbDialog;
    TitanHeartMonitorInfo titanHeartMonitorInfo;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                titanHeartMonitorInfo = (TitanHeartMonitorInfo) msg.obj;
                if (titanHeartMonitorInfo != null) {
                    if (titanHeartMonitorInfo.getDuration() <= 0) {
                        activityTitanHeartRateBinding.heartSwitch.setText(getString(R.string.switch_state_close));
                        activityTitanHeartRateBinding.intervalTime.setText(0 + "");
                    } else {
                        activityTitanHeartRateBinding.heartSwitch.setText(getString(R.string.switch_state_on));
                        activityTitanHeartRateBinding.intervalTime.setText(titanHeartMonitorInfo.getDuration() + "");
                    }
                    activityTitanHeartRateBinding.maxHeartRate.setText(titanHeartMonitorInfo.getHighThreshold() + "");
                    activityTitanHeartRateBinding.minHeartRate.setText(titanHeartMonitorInfo.getLowThreshold() + "");
                }


            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(TitanHeartChechActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(TitanHeartChechActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x44) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }


            }
        }
    };
    ActivityTitanHeartRateBinding activityTitanHeartRateBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityTitanHeartRateBinding=ActivityTitanHeartRateBinding.inflate(getLayoutInflater());
        setContentView(activityTitanHeartRateBinding.getRoot());
        activityTitanHeartRateBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityTitanHeartRateBinding.tool);
        getSupportActionBar().setTitle(null);
        activityTitanHeartRateBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(TitanHeartChechActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.heart_rate_check, new TitanHeartInfoCallback() {
                @Override
                public void titanHeartInfo(TitanHeartMonitorInfo titanHeartMonitorInfo) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.obj = titanHeartMonitorInfo;
                        mHandler.sendMessage(message);
                    }

                }

                @Override
                public void mutualFail(int i) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }
                }
            });
        }
        activityTitanHeartRateBinding.heartSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (heartDialog == null) {
                        heartDialog = new SwitchDialog(TitanHeartChechActivity.this);
                        heartDialog.setSelectListener(new SwitchDialog.SelectListener() {
                            @Override
                            public void selectData(String sex) {

                                if (sex.equalsIgnoreCase(getString(R.string.switch_state_on))) {
                                    activityTitanHeartRateBinding.intervalTime.setText(10 + "");
                                    if (waitingDialog == null) {
                                        waitingDialog = new WaitingDialog(TitanHeartChechActivity.this);
                                    }
                                    if (!waitingDialog.isShowing()) {
                                        waitingDialog.show();
                                    }
                                    if (titanHeartMonitorInfo == null) {
                                        titanHeartMonitorInfo = new TitanHeartMonitorInfo();
                                    }
                                    titanHeartMonitorInfo.setDuration(10);
                                    EABleManager.getInstance().setTitanAutoHeartCheck(titanHeartMonitorInfo, new GeneralCallback() {
                                        @Override
                                        public void result(boolean b, int i) {
                                            if (mHandler != null) {
                                                mHandler.sendEmptyMessage(0x44);
                                            }
                                        }

                                        @Override
                                        public void mutualFail(int i) {
                                            if (mHandler != null) {
                                                mHandler.sendEmptyMessage(0x43);
                                            }
                                        }
                                    });

                                } else {
                                    if (waitingDialog == null) {
                                        waitingDialog = new WaitingDialog(TitanHeartChechActivity.this);
                                    }
                                    if (!waitingDialog.isShowing()) {
                                        waitingDialog.show();
                                    }
                                    activityTitanHeartRateBinding.intervalTime.setText(0 + "");
                                    activityTitanHeartRateBinding.heartSwitch.setText(getText(R.string.switch_state_close));
                                    if (titanHeartMonitorInfo == null) {
                                        titanHeartMonitorInfo = new TitanHeartMonitorInfo();
                                    }
                                    titanHeartMonitorInfo.setDuration(0);
                                    EABleManager.getInstance().setTitanAutoHeartCheck(titanHeartMonitorInfo, new GeneralCallback() {
                                        @Override
                                        public void result(boolean b, int i) {
                                            if (mHandler != null) {
                                                mHandler.sendEmptyMessage(0x44);
                                            }
                                        }

                                        @Override
                                        public void mutualFail(int i) {
                                            if (mHandler != null) {
                                                mHandler.sendEmptyMessage(0x43);
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                    if (!heartDialog.isShowing()) {
                        heartDialog.show();
                    }

                }
            }
        });
        activityTitanHeartRateBinding.maxHeartRate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (maxDialog == null) {
                        maxDialog = new MonthDialog(TitanHeartChechActivity.this, getString(R.string.max_heart_rate));
                        maxDialog.setSelectListener(new MonthDialog.SelectListener() {
                            @Override
                            public void selectData(int sex) {
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanHeartChechActivity.this);
                                }
                                if (!waitingDialog.isShowing()) {
                                    waitingDialog.show();
                                }
                                if (titanHeartMonitorInfo == null) {
                                    titanHeartMonitorInfo = new TitanHeartMonitorInfo();
                                }
                                titanHeartMonitorInfo.setHighThreshold(sex);
                                EABleManager.getInstance().setTitanAutoHeartCheck(titanHeartMonitorInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean b, int i) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int i) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x43);
                                        }
                                    }
                                });
                            }
                        });
                    }
                    if (!maxDialog.isShowing()) {
                        maxDialog.show();
                    }
                }
            }
        });
        activityTitanHeartRateBinding.startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    if (notDisturbDialog == null) {
                        notDisturbDialog = new NotDisturbDialog(TitanHeartChechActivity.this, getString(R.string.start_time));
                        notDisturbDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                            @Override
                            public void selectData(int hour, int minute) {
                                activityTitanHeartRateBinding.startTime.setText(hour + ":" + minute);
                                if (waitingDialog == null) {
                                    waitingDialog = new WaitingDialog(TitanHeartChechActivity.this);
                                }
                                if (!waitingDialog.isShowing()) {
                                    waitingDialog.show();
                                }
                                if (titanHeartMonitorInfo == null) {
                                    titanHeartMonitorInfo = new TitanHeartMonitorInfo();
                                }
                                titanHeartMonitorInfo.setStartHour(hour);
                                titanHeartMonitorInfo.setStartMinute(minute);
                                EABleManager.getInstance().setTitanAutoHeartCheck(titanHeartMonitorInfo, new GeneralCallback() {
                                    @Override
                                    public void result(boolean b, int i) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x44);
                                        }
                                    }

                                    @Override
                                    public void mutualFail(int i) {
                                        if (mHandler != null) {
                                            mHandler.sendEmptyMessage(0x43);
                                        }
                                    }
                                });

                            }
                        });
                    }
                    if (!notDisturbDialog.isShowing()) {
                        notDisturbDialog.show();
                    }
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (heartDialog != null) {
            heartDialog.dismiss();
            heartDialog.destroyDialog();
            heartDialog = null;
        }

        if (monthDialog != null) {
            monthDialog.dismiss();
            monthDialog.destroyDialog();
            monthDialog = null;
        }
        if (maxDialog != null) {
            maxDialog.dismiss();
            maxDialog.destroyDialog();
            maxDialog = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }

        super.onDestroy();
    }
}
